#!/usr/bin/env python
# saga.py
"""
This program is a simple simulation of the game 'Saga'
by Wolgang Kramer and Horst-Rainer Rosner.

Ian Mahoney 2010, GPL
"""

def sumcards(n):
    """
    Adds up the total strength of a list of cards.
    
    Returns a single number.
    
    >>> a = [1, 6, 9, 9, 12, 14, 16, 18, 23, 27, 27, 28]
    >>> sumcards(a)
    30
    """
    a = 0
    for i in n:
        i = i%5
        a += i
    return a

class Game:
    def __init__(self, basic_setup = True):
        self.pictures = \
        {1:'blue 1',     2:'blue 2',     3:'blue 3',     4:'blue 4',\
         6:'green 1',    7:'green 2',    8:'green 3',    9:'green 4',\
         11:'yellow 1',  12:'yellow 2',  13:'yellow 3',  14:'yellow 4',\
         16:'red 1',     17:'red 2',     18:'red 3',     19:'red 4',\
         21:'purple 1',  22:'purple 2',  23:'purple 3',  24:'purple 4',
         26:'gold 1',    27:'gold 2',    28:'gold 3',    29:'gold 4'}

        self.tree = [1, 6, 9, 9, 12, 14, 16, 18, 23, 27, 27, 28]
        self.castle = [2, 2, 3, 8, 11, 16, 19, 19, 22, 24, 26, 28]
        self.sword = [1, 3, 7, 9, 12, 12, 13, 18, 21, 26, 29, 29]
        self.cross = [2, 4, 6, 8, 13, 17, 17, 18, 21, 24, 24, 26]
        self.lion = [3, 7, 7, 8, 11, 14, 14, 16, 21, 23, 27, 29]
        # self.missing = [1, 4, 4, 6, 11, 13, 17, 19, 22, 22, 23, 28]
        if basic_setup == True:
            SeaLand = Kingdom('blue', [19, 3])
            ForestLand = Kingdom('green', [2, 8])
            FieldLand = Kingdom('yellow', [28, 11])
            CastleLand = Kingdom('red', [24, 16])
            ShadowLand = Kingdom('purple', [2, 22])
            GoldLand = Kingdom('gold', [19, 26])
            player_1 = Player('Me', self.tree)
            player_2 = Player('them', self.sword)
        
        self.kingdom_list = [SeaLand, ForestLand, FieldLand, CastleLand, ShadowLand, GoldLand]
        self.player_list = [player_1, player_2]
        
        def help(player):
            print 'type \'hand\' to view your hand.'
            print 'type \'kingdoms\' to view all kingdoms in play.'
            print 'type \'attacks\' to view all attacks in play.'
            print 'type \'play\' to take your turn.'
        
        def hand(player):
            player.show_Hand(self)
        
        def kingdoms(player):
            for kingdom in self.kingdom_list:
                print kingdom
        
        def attacks(player):
            print player.attacks
        
        def play():
            None #        FIX LATER
        
        self.commands = {'help' : help,
                         'hand' : hand,
                         'kingdoms' : kingdoms,
                         'attacks' : attacks,
                         'play' : play}
        
        def turn(player):
            command = raw_input("Enter a command.  Type help for assistance.")
        
        def make_attack(player, card, color):
            player.removeCard(card)
            player.moreAttack(card, color)
            
            for kingdom in self.kingdom_list:
                if (kingdom.color == color) and (kingdom.defense < sumcards(player.attacks[color])): 
                    if kingdom.owner in self.player_list:
                        for card in kingdom.defenders:
                            player.addCard(card)
                    
                    kingdom.conquer(player, player.attacks[color])
            
            

class Player:
    def __init__(self, name, hand):
        """
        Creates a player with a name and a list of knight cards in hand.
        
        Requires 2 inputs: a string to be the name of the player, and a list of knights.
        
        >>> Player_1 = Player('Joe', game.tree)
        >>> Player_1.name
        'Joe'
        >>> Player_1.hand
        [1, 6, 9, 9, 12, 14, 16, 18, 23, 27, 27, 28]
        """
        self.name = name
        self.hand = hand
        self.fame = 0
        self.attacks = {}
    
    def moreFame(self, n):
        self.fame += n
    
    def lessFame(self, n):
        self.fame -= n
    
    def addCard(self, card):
        self.hand.append(card)
    
    def removeCard(self, card):
        self.hand.remove(card)
    
    def showHand(self, game):
        for i in self.hand:
            print game.pictures[i]
    
    def startAttack(self, card, game):
        """
        Creates an attacking force with the card given.
        
        The key for the attack in self.attacks is the color of the attacker.
        The item is the strength of the attack
        
        >>> self.startAttack(1)
        >>> self.attacks
        {'blue': 1}
        """
        a = game.pictures[card]
        b = ' %s' %a[-1]
        c = a.strip(b)
        
        a.attacks[c] = [card%5]
    
    def moreAttack(self, color, card):
        self.attacks[color].append(card)


class Kingdom:
    def __init__(self, color, defenders):
        self.color = color
        self.defenders = defenders
        self.defense = sumcards(defenders)
        self.owner = None
    
    def __str__(self):
        print self.color + ": " + self.owner + ", " + self.defense
    
    def conquer(self, player, attackers):
        self.owner = player
        self.defenders = attackers
        self.defense = sumcards(attackers)


def main():
    game = Game()
    
    

#main()

def _test():
    from doctest import testmod
    testmod()
if __name__=="__main__":
    _test()